//
//  TUIVideoMessageCell.m
//  UIKit
//
//  Created by annidyfeng on 2019/5/30.
//

#import "LGVideoMessageCell.h"
#import "TUIDefine.h"

@interface LGVideoMessageCell ()

@property (nonatomic, strong) UIView *animateHighlightView;

@end


@implementation LGVideoMessageCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.bubbleView addSubview:self.thumbView];
        [self.bubbleView addSubview:self.playView];
        [self.bubbleView addSubview:self.durationLabel];
        [self.thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self.bubbleView);
        }];
        [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self.bubbleView);
            make.width.height.equalTo(@(35));
        }];
        [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bubbleView);
            make.left.equalTo(self.bubbleView).offset(viewPix(10));
            make.right.equalTo(self.bubbleView).offset(-viewPix(10));
            make.height.equalTo(@(20));
        }];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectMessage)];
        [self.bubbleView addGestureRecognizer:tapGesture];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    NSDictionary *contentDic = dataDic[@"content"];
    [self.thumbView sd_setImageWithURL:[NSURL URLWithString:LGNSString(contentDic[@"original_thumb_url"])]];
    self.durationLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)[contentDic[@"video_second"] integerValue] / 60, (long)[contentDic[@"video_second"] integerValue] % 60];;
    CGFloat thumHeight = [contentDic[@"thumb_height"] floatValue];
    CGFloat thumWidth = [contentDic[@"thumb_width"] floatValue];
    if(thumHeight > thumWidth){
        thumWidth = thumWidth / thumHeight * TVideoMessageCell_Image_Height_Max;
        thumHeight = TVideoMessageCell_Image_Height_Max;
    } else {
        thumHeight = thumHeight / thumWidth * TVideoMessageCell_Image_Width_Max;
        thumWidth = TVideoMessageCell_Image_Width_Max;
    }
    [self.bubbleView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(thumWidth));
        make.height.equalTo(@(thumHeight));
    }];
}

-(void)onSelectMessage{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showVideoMessage:cell:)]) {
        [self.delegate showVideoMessage:self.dataDic cell:self];
    }
}

-(UIImageView *)thumbView{
    if(!_thumbView){
        _thumbView = [[UIImageView alloc]init];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.backgroundColor = [UIColor clearColor];
        _thumbView.cornerRidus = 5.0;
    }
    return _thumbView;
}

-(UIImageView *)playView{
    if(!_playView){
        _playView = [[UIImageView alloc]initWithImage:[[TUIImageCache sharedInstance] getResourceFromCache:TUIChatImagePath(@"play_normal")]];
        _playView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _playView;
}

-(UILabel *)durationLabel{
    if(!_durationLabel){
        _durationLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _durationLabel;
}

-(UILabel *)progressLabel{
    if(!_progressLabel){
        _progressLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFont(15) textAlignment:NSTextAlignmentCenter lines:1];
        _progressLabel.backgroundColor = TVideoMessageCell_Progress_Color;
        _progressLabel.cornerRidus = 5;
        _progressLabel.hidden = YES;
    }
    return _progressLabel;
}

@end
